﻿import QtQuick 2.0

//虚拟摇杆
Image {
    id: snakerBg
    source: "qrc:/img/img/rockerBg.png"

    signal sendAngle(var angle)
    signal stop()

    property alias snakerMouse: snakerMouse
    property alias snakerSnaker: snaker


    Image{
        id: snaker
        x: (parent.width-width)/2
        y: (parent.height-height)/2
        source: "qrc:/img/img/rocker.png"
    }

    MouseArea{
        id: snakerMouse
        anchors.fill: parent
        drag.target: snaker
        drag.minimumX: 0
        drag.maximumX: snakerBg.width-snaker.width
        drag.minimumY: 0
        drag.maximumY: snakerBg.height-snaker.height
        preventStealing: true

        onReleased: {
            snaker.x = (snakerBg.width-snaker.width)/2
            snaker.y = (snakerBg.height-snaker.height)/2

            stop()
        }

        onPressed: {
            snaker.x = mouseX - snaker.width/2
            snaker.y = mouseY - snaker.height/2
        }

        onPositionChanged: {
            //计算snaker显示

            var centerX = snakerBg.width/2
            var centerY = snakerBg.height/2

            var snakerCX = snaker.x + snaker.width/2
            var snakerCY = snaker.y + snaker.height/2

            var radian = Math.atan2(snakerCY - centerY, snakerCX - centerX )

            var angle = 180 / Math.PI * radian

            sendAngle(angle)
        }
    }
}

